﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using NGE;

namespace NGE.ToNAnimation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Image imgbuff;

        NAnimation nanibuff;

        Rectangle sltzone;

        int basex = 0;
        int basey = 0;

        int mousex = 0;
        int mousey = 0;

        bool drawing;

        void updatesltzone()
        {
            sltzone.X = int.Parse(textBox1.Text);
            sltzone.Y = int.Parse(textBox2.Text);
            sltzone.Width = int.Parse(textBox4.Text);
            sltzone.Height = int.Parse(textBox3.Text);
            sltzone.X += basex;
            sltzone.Y += basey;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nimg|*.nimg|*.png|*.png|*.gif|*.gif";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                if (fd.FilterIndex == 1)
                {
                    NImage nimgbuff = ((NImage)Util.LoadObject(fd.FileName));
                    nimgbuff.Init();
                    imgbuff = nimgbuff.MemImage;
                }
                else if (fd.FilterIndex == 2)
                {
                    imgbuff = Image.FromFile(fd.FileName);
                }

                textBox7.Text = textBox1.Text = "0";
                textBox8.Text = textBox2.Text = "0";
                textBox5.Text = textBox4.Text = imgbuff.Width.ToString();
                textBox6.Text = textBox3.Text = imgbuff.Height.ToString();

                //pictureBox1.Size = imgbuff.Size;
                pictureBox1.Refresh();
                panel1.Refresh();
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (imgbuff != null)
            {
                g.DrawImage(imgbuff, basex, basey);
                label13.Text = "素材尺寸：" + imgbuff.Size + " 鼠标坐标：" + mousex + ", " + mousey;
            }


            updatesltzone();
            g.DrawRectangle(Pens.Red, sltzone);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            mousex = e.X - basex;
            mousey = e.Y - basey;

            if (drawing)
            {
                int x = int.Parse(textBox1.Text);
                int y = int.Parse(textBox2.Text);

                if (mousex > x && mousey > y)
                {
                    textBox5.Text = textBox4.Text = (mousex - x).ToString();
                    textBox6.Text = textBox3.Text = (mousey - y).ToString();

                }
            }

            pictureBox1.Refresh();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                textBox1.Text = mousex.ToString();
                textBox2.Text = mousey.ToString();
                drawing = true;
            }
            else if (e.Button == MouseButtons.Right)
            {
                int x = int.Parse(textBox1.Text);
                int y = int.Parse(textBox2.Text);

                if (mousex > x && mousey > y)
                {
                    textBox5.Text = textBox4.Text = (mousex - x).ToString();
                    textBox6.Text = textBox3.Text = (mousey - y).ToString();

                    drawing = false;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (nanibuff == null)
            {
                nanibuff = new NAnimation();
            }

            NImage frmnimg = new NImage();
            Rectangle rect = new Rectangle(int.Parse(textBox7.Text), int.Parse(textBox8.Text), int.Parse(textBox5.Text), int.Parse(textBox6.Text));
            Bitmap frmbmp = new Bitmap(rect.Width, rect.Height);
            Graphics g = Graphics.FromImage(frmbmp);
            g.DrawImage(imgbuff, new Rectangle(new Point(), rect.Size), rect, GraphicsUnit.Pixel);
            frmnimg.GetData(frmbmp);
            nanibuff.AddFrame(frmnimg, int.Parse(textBox10.Text));

            trackBar1.Maximum = nanibuff.FramesCount;
            label12.Text = "/" + trackBar1.Maximum;

            pictureBox2.Refresh();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            updatetrack();
        }

        void updatetrack()
        {
            textBox12.Text = (trackBar1.Value).ToString();
            
            int findex = trackBar1.Value - 1;
            if (nanibuff == null)
            {
                return;
            }
            nanibuff.GotoFrame(findex);
            textBox10.Text = nanibuff.CurrentFrameDelay + "";
            pictureBox2.Refresh();
        }

        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            if (nanibuff == null)
            {
                return;
            }

            int findex = trackBar1.Value - 1;
            Graphics g = e.Graphics;
            

            if (findex < nanibuff.FramesCount)
            {
                nanibuff.CurrentFrame.Init();
                g.DrawImage(nanibuff.CurrentFrame.MemImage, new Point());
            }

            g.DrawLine(Pens.Red, new Point((int)nanibuff.Offset.X - 5, (int)nanibuff.Offset.Y), new Point((int)nanibuff.Offset.X + 5, (int)nanibuff.Offset.Y));
            g.DrawLine(Pens.Red, new Point((int)nanibuff.Offset.X, (int)nanibuff.Offset.Y - 5), new Point((int)nanibuff.Offset.X, (int)nanibuff.Offset.Y + 5));
        }

        private void play_Click(object sender, EventArgs e)
        {
            int findex = trackBar1.Value - 1;
            timer1.Interval = nanibuff.CurrentFrameDelay;
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            if (trackBar1.Value < trackBar1.Maximum)
            {
                trackBar1.Value++;
                updatetrack();

                timer1.Interval = nanibuff.CurrentFrameDelay;
                timer1.Enabled = true;

            }
            else if (checkBox1.Checked)
            {
                trackBar1.Value = 1;
                updatetrack();
                timer1.Interval = nanibuff.CurrentFrameDelay;
                timer1.Enabled = true;
            }
            else
            {
                trackBar1.Value = 1;
                updatetrack();

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                int fcount = int.Parse(textBox13.Text);
                int step = int.Parse(textBox6.Text) / fcount;
                for (int i = 0; i < fcount; i++)
                {
                    if (nanibuff == null)
                    {
                        nanibuff = new NAnimation();
                    }

                    NImage frmnimg = new NImage();
                    Rectangle rect = new Rectangle(0, i * step, int.Parse(textBox5.Text), step);

                    Bitmap frmbmp = new Bitmap(rect.Width, rect.Height);
                    Graphics g = Graphics.FromImage(frmbmp);

                    g.DrawImage(imgbuff, new Rectangle(new Point(), rect.Size), rect, GraphicsUnit.Pixel);
                    frmnimg.GetData(frmbmp);
                    nanibuff.AddFrame(frmnimg, int.Parse(textBox10.Text));

                    trackBar1.Maximum = nanibuff.FramesCount;
                    label12.Text = "/" + trackBar1.Maximum;

                    pictureBox2.Refresh();
                }
            }
            else
            { 
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            nanibuff = new NAnimation();
            updatetrack();
            pictureBox2.Refresh();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            SaveFileDialog fd = new SaveFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nani|*.nani";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                Util.SaveObject(fd.FileName, nanibuff);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.nani|*.nani";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                nanibuff = (NAnimation)Util.LoadObject(fd.FileName);
                trackBar1.Maximum = nanibuff.FramesCount;
                label12.Text = "/" + trackBar1.Maximum;

                label14.Text = "X: " + nanibuff.Offset.X + " Y: " + nanibuff.Offset.Y;

                pictureBox2.Refresh();
            }

        }

        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                nanibuff.Offset.X = e.X;
                nanibuff.Offset.Y = e.Y;

                label14.Text = "X: " + e.X + " Y: " + e.Y;

                pictureBox2.Refresh();
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            nanibuff.SetFrame(trackBar1.Value - 1, int.Parse(textBox10.Text));
        }
    }
}
